package com.hardroid.workload.ui.fragment;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.NewEmployerActivity;
import com.hardroid.workload.ui.tablet.DashboardMultiPaneActivity;
import com.hardroid.workload.utils.EmployerAdapter;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.workcalc.ObsvProject.ProjectStatus;

public class EmployerFragment extends ListFragment {

	
	
	public Button addButton;
	public EmployerAdapter adapter;
	public final static int REMOVE_EMPLOYER = 0;
	public int selectionPos = -999;

	public TextView idText;
	public TextView nameText;
	public TextView divisionText;
	public TextView unitText;
	public TextView jobText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		try {
			// if (Singleton.currentProject.lEmployer.size()!=0){
			// if (Singleton.employerAdapter==null){
			adapter = Singleton.employerAdapter;

//			setListAdapter(adapter);

			// }else{
			setListAdapter(Singleton.employerAdapter);
			// }

			// }
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_employer, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		idText = (TextView) getView().findViewById(R.id.list_item_employer_id);
		nameText = (TextView) getView().findViewById(
				R.id.list_item_employer_name);
		divisionText = (TextView) getView().findViewById(
				R.id.list_item_employer_division);
		unitText = (TextView) getView().findViewById(
				R.id.list_item_employer_unit);
		jobText = (TextView) getView()
				.findViewById(R.id.list_item_employer_job);

		idText.setText("No");
		nameText.setText("Nama");
		divisionText.setText("Divisi");
		unitText.setText("Unit");
		jobText.setText("Pekerjaan");

		addButton = (Button) getView().findViewById(
				R.id.employer_add_new_employer);
		addButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Singleton.currentProject.projectStatus!=ProjectStatus.PROJECT_FINISHED){
					
					if (Singleton.isDayReferenceValid()){												
						startActivity(new Intent(getActivity(),
								NewEmployerActivity.class));
					}else{
						getActivity().showDialog(DashboardMultiPaneActivity.TIME_PICKER_START_DIALOG);
					}	
					
						
				}else{
					Toast.makeText(getActivity(), "Maaf, Anda tidak dapat menambah employer baru", Toast.LENGTH_SHORT).show();
				}
				
			}
		});

		registerForContextMenu(getListView());
	}

	public void removeEmployer() {

		Singleton.currentProject.removeResponden(selectionPos);

		// commit
		Singleton.saveProject(Singleton.currentProject);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (Singleton.currentProject.projectStatus!=ProjectStatus.PROJECT_FINISHED){
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle("Menu");
			menu.add(0, REMOVE_EMPLOYER, 0, "Hapus");
			selectionPos = info.position;			
		}
		
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == REMOVE_EMPLOYER) {
			removeEmployer();
			return true;
		}
		return false;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		l.setItemChecked(position, true);
	}
	

}